# PowerShell 配置脚本
# 设置 UTF-8 支持和优化环境

Write-Host "🔧 配置 PowerShell 环境..." -ForegroundColor Cyan

# 获取配置文件路径
$profilePath = $PROFILE.CurrentUserCurrentHost
$profileDir = Split-Path $profilePath -Parent

# 创建目录
if (-not (Test-Path $profileDir)) {
    New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
    Write-Host "📁 创建配置目录: $profileDir" -ForegroundColor Blue
}

# 备份现有配置
if (Test-Path $profilePath) {
    $backupPath = "$profilePath.backup.$(Get-Date -Format 'yyyyMMdd_HHmmss')"
    Copy-Item $profilePath $backupPath -Force
    Write-Host "💾 备份现有配置: $backupPath" -ForegroundColor Yellow
}

# 创建新的配置文件
$profileContent = @'
# PowerShell 配置文件 - UTF-8 支持
# 创建时间: $(Get-Date)

# 设置控制台编码为 UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8

# 设置默认文件编码
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$PSDefaultParameterValues['Set-Content:Encoding'] = 'utf8'
$PSDefaultParameterValues['Get-Content:Encoding'] = 'utf8'

# 设置代码页为 UTF-8
try {
    chcp 65001 | Out-Null
    Write-Host "✅ 代码页设置为 UTF-8 (65001)" -ForegroundColor Green
} catch {
    Write-Warning "⚠️  无法更改代码页，可能需要管理员权限"
}

# 创建简单的提示符（避免 Unicode 字符）
function prompt {
    $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = [Security.Principal.WindowsPrincipal] $identity
    $adminSuffix = ""
    if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
        $adminSuffix = " [ADMIN]"
    }

    $path = $PWD.Path
    if ($path.Length -gt 40) {
        $path = "..." + $path.Substring($path.Length - 37)
    }

    "PS $path$adminSuffix> "
}

# 设置有用的别名
try {
    Set-Alias -Name "ll" -Value "Get-ChildItem" -Option AllScope -Description "长格式列表" -ErrorAction SilentlyContinue
    Set-Alias -Name "la" -Value "Get-ChildItem -Force" -Option AllScope -Description "显示所有文件" -ErrorAction SilentlyContinue
    Set-Alias -Name "grep" -Value "Select-String" -Option AllScope -Description "文本搜索" -ErrorAction SilentlyContinue
    Write-Host "✅ 别名设置完成" -ForegroundColor Green
} catch {
    Write-Warning "⚠️  部分别名设置失败"
}

# 显示欢迎信息
Write-Host ""
Write-Host "🎉 PowerShell 环境配置完成！" -ForegroundColor Cyan
Write-Host "📊 PowerShell 版本: $($PSVersionTable.PSVersion)" -ForegroundColor Blue
Write-Host "🔤 输出编码: $([Console]::OutputEncoding.EncodingName)" -ForegroundColor Blue
Write-Host "🔤 输入编码: $([Console]::InputEncoding.EncodingName)" -ForegroundColor Blue
Write-Host ""
Write-Host "💡 现在可以正常使用 Unicode 字符了！" -ForegroundColor Yellow
'@

# 保存配置文件
$profileContent | Out-File -FilePath $profilePath -Encoding utf8 -Force

Write-Host ""
Write-Host "✅ PowerShell 配置文件已保存到: $profilePath" -ForegroundColor Green
Write-Host "🔄 请重新启动 PowerShell 以使配置生效" -ForegroundColor Yellow
Write-Host ""

# 测试配置
Write-Host "🧪 测试 Unicode 支持:" -ForegroundColor Cyan
Write-Host "  英文: Hello World" -ForegroundColor White
Write-Host "  中文: 你好世界" -ForegroundColor Green
Write-Host "  日文: こんにちは" -ForegroundColor Magenta
Write-Host ""

# 测试 Python Unicode
Write-Host "🐍 测试 Python Unicode 支持:" -ForegroundColor Cyan
try {
    $pythonTest = python -c "print('Python: 你好 🎉')" 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ Python Unicode 支持正常" -ForegroundColor Green
    } else {
        Write-Host "❌ Python 未安装或配置有问题" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ Python 未找到" -ForegroundColor Red
}

Write-Host ""
Write-Host "🎯 下一步操作:" -ForegroundColor Yellow
Write-Host "1. 关闭当前 PowerShell 窗口" -ForegroundColor White
Write-Host "2. 按 Win+R，输入 'wt' 启动 Windows Terminal" -ForegroundColor White
Write-Host "3. 测试: python -c \"print('✅ 你好!')\"" -ForegroundColor White
Write-Host ""